package com.ruoyi.teaching.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.weChatRobot.utils.WechatRobotUtil;
import com.ruoyi.teaching.domain.Focus;
import com.ruoyi.teaching.service.IFocusService;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.TeachingTraceMapper;
import com.ruoyi.teaching.domain.TeachingTrace;
import com.ruoyi.teaching.service.ITeachingTraceService;
import org.springframework.util.CollectionUtils;

/**
 * 教学跟踪Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-23
 */
@Service
public class TeachingTraceServiceImpl implements ITeachingTraceService 
{
    @Autowired
    private TeachingTraceMapper teachingTraceMapper;

    @Autowired
    private IFocusService focusService;
    /**
     * 定时提醒老师跟踪
     */
    @Override
    public void reminderTracking(){
        // 用于存教学跟踪记录的集合
        List<TeachingTrace> reminderTeachingTraces = new ArrayList<>();
        List<Focus> focusStudents = focusService.selectFocusList(new Focus());
        AssertUtil.notNull(focusStudents,"查询重点关注失败");
        // 获取重点关注学生id集合
        List<Long> focusStudentIds = focusStudents.stream().map(Focus::getStudentId).collect(Collectors.toList());
        // 查询所有跟踪记录
        List<TeachingTrace> teachingTraces = teachingTraceMapper.selectTeachingTraceList(new TeachingTrace());
        AssertUtil.notNull(teachingTraces,"查询跟踪记录失败");
        // 根据学生id把跟踪记录进行分组
        Map<Long, List<TeachingTrace>> teachingTracesByStudentId = teachingTraces.stream()
                .collect(Collectors.groupingBy(TeachingTrace::getStudentId));
        // 找出超过七天没有追踪的记录
        for (Long focusStudentId : focusStudentIds) {
            List<TeachingTrace> teachingTraceByOne = teachingTracesByStudentId.get(focusStudentId);
            for (TeachingTrace teachingTrace : teachingTraceByOne) {
                if (teachingTrace.getTraceTimes() != teachingTraceByOne.size()){
                    continue;
                }
                Date createTime = teachingTrace.getCreateTime();
                long between = DateUtil.between(createTime, new Date(), DateUnit.DAY);
                if (between > Constants.OVER_TRACE_DAYS) {
                    reminderTeachingTraces.add(teachingTrace);
                }
            }
        }
        if (CollUtil.isNotEmpty(reminderTeachingTraces)){
            StringBuilder stringBuilder = new StringBuilder();
            for (TeachingTrace reminderTeachingTrace : reminderTeachingTraces) {
                stringBuilder.append(reminderTeachingTrace.getStudentName()).append(",");
            }
            String reminderTeacherTrace = String.format(Constants.REMINDER_TEACHER_TRACE, stringBuilder);
            WechatRobotUtil.sendTextMessage(reminderTeacherTrace,new ArrayList());
        }
    }

    /**
     * 批量插入教学跟踪
     * @param teachingTraces
     * @return
     */
    @Override
    public int insertTeachingTraceList(List<TeachingTrace> teachingTraces) {
        for (TeachingTrace teachingTrace : teachingTraces) {
            int result = insertTeachingTrace(teachingTrace);
            if (result <= 0){
                return 0;
            }
        }
        return 1;
    }

    /**
     * 查询教学跟踪
     * 
     * @param id 教学跟踪主键
     * @return 教学跟踪
     */
    @Override
    public TeachingTrace selectTeachingTraceById(Long id)
    {
        return teachingTraceMapper.selectTeachingTraceById(id);
    }

    /**
     * 查询教学跟踪列表
     * 
     * @param teachingTrace 教学跟踪
     * @return 教学跟踪
     */
    @Override
    public List<TeachingTrace> selectTeachingTraceList(TeachingTrace teachingTrace)
    {
        return teachingTraceMapper.selectTeachingTraceList(teachingTrace);
    }

    /**
     * 新增教学跟踪
     * 
     * @param teachingTrace 教学跟踪
     * @return 结果
     */
    @Override
    public int insertTeachingTrace(TeachingTrace teachingTrace)
    {
        // 1.参数校验
        // 3.业务实现
        // 3.1 设置当前登录人为跟踪老师
        teachingTrace.setTeacherId(SecurityContextHolder.getUserId());
        teachingTrace.setTeacherName(SecurityContextHolder.getUserName());

        teachingTrace.setCreateTime(DateUtils.getNowDate());
        // 设置逻辑删除
        teachingTrace.setDeleted(1L);
        // 3.2查询所有该学生的跟踪记录
        List<TeachingTrace> teachingTraces = teachingTraceMapper.selectTeachingTraceByStudentId(teachingTrace.getStudentId());
        if (Objects.isNull(teachingTraces) || CollectionUtils.isEmpty(teachingTraces)){
            // 3.2.1 如果没有则直接新增并把跟踪次数设为1
            teachingTrace.setTraceTimes(1L);
            return teachingTraceMapper.insertTeachingTrace(teachingTrace);
        }
        // 获取上次的跟踪次数
        TeachingTrace teachingTraceFromDb = teachingTraces.get(0);
        AssertUtil.notNull(teachingTraceFromDb,"教学跟踪记录异常");
        teachingTrace.setTraceTimes(teachingTraceFromDb.getTraceTimes() + 1);
        return teachingTraceMapper.insertTeachingTrace(teachingTrace);
    }

    /**
     * 修改教学跟踪
     * 
     * @param teachingTrace 教学跟踪
     * @return 结果
     */
    @Override
    public int updateTeachingTrace(TeachingTrace teachingTrace)
    {
        teachingTrace.setUpdateTime(DateUtils.getNowDate());
        return teachingTraceMapper.updateTeachingTrace(teachingTrace);
    }

    /**
     * 批量删除教学跟踪
     * 
     * @param ids 需要删除的教学跟踪主键
     * @return 结果
     */
    @Override
    public int deleteTeachingTraceByIds(Long[] ids)
    {
        return teachingTraceMapper.deleteTeachingTraceByIds(ids);
    }

    /**
     * 删除教学跟踪信息
     * 
     * @param id 教学跟踪主键
     * @return 结果
     */
    @Override
    public int deleteTeachingTraceById(Long id)
    {
        return teachingTraceMapper.deleteTeachingTraceById(id);
    }
}
